capture log close
#delimit;
clear;
set more 1;
set memory 50m;
set matsize 500;

log using "gsoep-suicide-data-2003-restat.log", replace;

display "This version: $S_DATE $S_TIME";

global rootgsoep "/Users/sascha/work/gsoep/GSOEP26/";
use persnr psample using "$rootgsoep/ppfad.dta";

/***********************************************************************************************************/
/* keep West Germans (including foreigners), East Germans (sample 3) and refreshment samples 1998 and 2000 */
/***********************************************************************************************************/

tab psample;

/*                 Stichprobenart |      Freq.     Percent        Cum.
----------------------------------+-----------------------------------
            [1] A Deutsche (West) |      5,480       24.26       24.26
          [2] B Auslaender (West) |      1,519        6.72       30.98
             [3] C Deutsche (Ost) |      3,453       15.28       46.26
  [4] D Zuwanderer (West) 1984-93 |        789        3.49       49.76
            [5] E Ergaenzung 1998 |      1,332        5.90       55.65
            [6] F Ergaenzung 2000 |      8,006       35.44       91.09
[7] G Hocheinkommensbezieher 2002 |      2,013        8.91      100.00
----------------------------------+-----------------------------------
                            Total |     22,592      100.00
*/

keep if psample==1 | psample==2 | psample==3 | psample==4 | psample==5 | psample==6;


sort persnr;
save my_ppfad.dta, replace;

/********************************/
/* information from GSOEP files */
/********************************/

/* 2003 */
use persnr
	tp0901 /* religious orientation */
	tp1401 /* Essen oder trinken gehen (Café, Kneipe, Restaurant) */
	tp1402 /* Besuche Nachbarn,Freunde */
	tp1403 /* Besuche Familie,Verwandte */
	tp1407 /* Kirchgang, Besuch religioeser Veranstaltungen */
	using $rootgsoep/tp;

rename tp0901 relig;
label define religlbl 
1 "[1] catholic"
2 "[2] protestant"
3 "[3] other christian denomination"
4 "[4] other denomination"
5 "[5] no denomination or religion";
label values relig religlbl;

/* TP0901 
Do you belong to a church or religious community?
If yes, are you 
catholic ___
evangelical ___
member of another Christian community ___
member of another religious community ___ ____ TP0902 Please indicate
No, nondenominational ___  
. tab tp0901

                  Kirche, Religion |      Freq.     Percent        Cum.
-----------------------------------+-----------------------------------
                   -1 keine Angabe |        146        0.65        0.65
                      1 katholisch |      6,698       29.62       30.27
                     2 evangelisch |      7,525       33.28       63.55
3 Andere christl. Rel.gemeinschaft |        524        2.32       65.87
         4 Andere Rel.gemeinschaft |        881        3.90       69.76
                 5  Konfessionslos |      6,837       30.24      100.00
-----------------------------------+-----------------------------------
                             Total |     22,611      100.00
*/

#delimit cr

rename tp1401 leis_eatdrink /* Essen oder trinken gehen (Café, Kneipe, Restaurant) */
rename tp1402 leis_neighfriend /* Besuche Nachbarn,Freunde */
rename tp1403 leis_famrel /* Besuche Familie,Verwandte */
rename tp1407 leis_church /* Kirchgang, Besuch religioeser Veranstaltungen */

#delimit;

sort persnr;
save my_variables_2003.dta, replace;

merge 1:1 persnr using my_ppfad.dta;
tab _merge;
keep if _merge==3;
drop _merge;

/******************/
/* missing values */
/******************/

mvdecode _all, mv(-1);
mvdecode _all, mv(-2);
mvdecode _all, mv(-3);

/*********************/
/* some descriptives */
/*********************/

# delimit cr

matrix emptyrow = J(1,6,.)
matrix everyone = emptyrow
matrix everyone_short = emptyrow

foreach activity in church famrel neighfriend eatdrink {
  matrix everyone_`activity'_abs = J(3,6,.)
  matrix everyone_`activity'_rel = J(3,6,.)
  matrix emptyrow_`activity' = J(1,6,.)
  recode leis_`activity' -1 = .
  local rownum = 0
    foreach relig in 2 1 5 {
	local rownum = `rownum'+1
    count if leis_`activity' != . & relig == `relig'
	local total = r(N)
    count if leis_`activity' == 1 & relig == `relig'
	matrix everyone_`activity'_abs[`rownum', 1] = r(N)
    count if leis_`activity' <= 2 & relig == `relig'
	matrix everyone_`activity'_abs[`rownum', 2] = r(N)
    count if leis_`activity' <= 3 & relig == `relig'
	matrix everyone_`activity'_abs[`rownum', 3] = r(N)
    count if leis_`activity' <= 4 & relig == `relig'
	matrix everyone_`activity'_abs[`rownum', 4] = r(N)
    count if leis_`activity' <= 5 & relig == `relig'
	matrix everyone_`activity'_abs[`rownum', 5] = r(N)
    count if leis_`activity' != . & relig == `relig'
	matrix everyone_`activity'_abs[`rownum', 6] = r(N)
	
    matrix everyone_`activity'_rel[`rownum', 1] = everyone_`activity'_abs[`rownum', 1] / `total'
    matrix everyone_`activity'_rel[`rownum', 2] = everyone_`activity'_abs[`rownum', 2] / `total'
    matrix everyone_`activity'_rel[`rownum', 3] = everyone_`activity'_abs[`rownum', 3] / `total'
    matrix everyone_`activity'_rel[`rownum', 4] = everyone_`activity'_abs[`rownum', 4] / `total'
    matrix everyone_`activity'_rel[`rownum', 5] = everyone_`activity'_abs[`rownum', 5] / `total'
    matrix everyone_`activity'_rel[`rownum', 6] = everyone_`activity'_abs[`rownum', 6]
  }
  
  matrix rownames everyone_`activity'_rel = Protestant Catholic "No religion"
  matrix rownames emptyrow_`activity' = "`activity'"
  
}

foreach activity in church famrel neighfriend eatdrink {
  matrix everyone_short = everyone_short \ emptyrow_`activity' \ everyone_`activity'_rel
}

matrix colnames everyone_short = Daily Weekly Monthly Less Never Obs
matorder everyone_short, matrix(everyone_short) over ifcol(Weekly Monthly Obs)
matrix everyone_short_tbl = everyone_short
matrix list everyone_short_tbl

log close
